package nguyenquangminh.minigame;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;

public class Chicken {
	private BufferedImage image;
	private static final int Y = 30;
	protected static final int WIDTH = 82;
	protected static final int HEIGHT = 88;
	//private Image image;
	private int x = 0;
	private int dx = 1;
	private Game game;
	private ArrayList<Chicken> chickenList;
	private ArrayList<Egg> eggsList;
	private ArrayList<Shit> shitsList;
	private int count = 0;
	private int secondDelay = 5;
	private int second = 5;
	private int random = 0;
	
	// Constructor
	public Chicken() {
		URL imageUrl = this.getClass().getResource("/images/ga.png");
		try {
			image = ImageIO.read(imageUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		chickenList = new ArrayList<Chicken>();
		Random rand = new Random();
		x = rand.nextInt(game.S_WIDTH);
		eggsList = new ArrayList<Egg>();
		shitsList = new ArrayList<Shit>();
	} 
	
	// Move method
	public void move() {
		if (x + dx < 0) 
			dx = 1;
		if (x + dx > game.S_WIDTH-WIDTH)
			dx = -1;
		x = x + dx;
	}
	
	// Lay egg method
	public void layEgg(int second) {
		count++;
		if (count == 100) {
			secondDelay--;
			count = 0;
		}
		if (secondDelay == 0) {
			eggsList.add(new Egg(x, Y + this.HEIGHT));
			secondDelay = second;
		}
	}
	
	// Lay shit method
	public void layShit(int second) {
		count++;
		if (count == 100) {
			secondDelay--;
			count = 0;
		}
		if (secondDelay == 0) {
			shitsList.add(new Shit(x, Y + this.HEIGHT));
			secondDelay = second;
		}
	}
	
	// Get X coordinates
	public int getX() {
		return x;
	}
	// Get Y coordinates
	public int getY() {
		return Y;
	}
	// Get Image
	public Image getImage() {
		return image;
	}
	
	// Get list chickens
	public ArrayList<Chicken> getChickenList() {
        return chickenList;
    }
	// Get list eggs
	public ArrayList<Egg> getEggsList() {
        return eggsList;
    }
	// Get list shits
	public ArrayList<Shit> getShitsList() {
        return shitsList;
    }
	
	// Key pressed event
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_G) {
			Chicken c = new Chicken();
			chickenList.add(new Chicken());
		}
	}
	
	// Paint chicken
	public void paint(Graphics2D g2d) {
		g2d.drawImage(image, x, Y, WIDTH, HEIGHT, null);
		// Set value random for eggs and shits
		Random rand = new Random();
		random = rand.nextInt(10);
		if (random == 5)
			this.layShit(second);
		else
			this.layEgg(second);
		// Paint eggs
		for (int i = 0; i < eggsList.size(); i++ ) {
	            Egg e = (Egg) eggsList.get(i);
	            if (e.isVisible == true)
	            	e.paint(g2d);
		}
		for (int i = 0; i < eggsList.size(); i++) {
			Egg e = (Egg) eggsList.get(i);
			if (e.isVisible == true)
				e.move();
			else
				eggsList.remove(i);
        }
		// Paint shits
		for (int i = 0; i < shitsList.size(); i++ ) {
            Shit s = (Shit) shitsList.get(i);
            if (s.isVisible == true)
            	s.paint(g2d);
		}
		for (int i = 0; i < shitsList.size(); i++) {
			Shit s = (Shit) shitsList.get(i);
			if (s.isVisible == true)
				s.move();
			else
				shitsList.remove(i);
	    }
	} // End Paint chicken method
} // End Chicken class
